package com.rockbb.common.sso.cookie;

import java.util.HashMap;
import java.util.Map;

import com.rockbb.common.sso.session.SessionConstants;

public class SessionCookies
{
	public static final String[] KEYS = {
		SessionConstants.COOKIE_KEY + "_s",
		SessionConstants.COOKIE_KEY + "_u",
		SessionConstants.COOKIE_KEY + "_l",
		SessionConstants.COOKIE_KEY + "_t"
	};
	public static final int KEY_SESSIONID = 0;
	public static final int KEY_USERID = 1;
	public static final int KEY_LANGID = 2;
	public static final int KEY_STYLEID = 3;

	private Map<String, String> cookies;

	public SessionCookies() 
	{
		cookies = new HashMap<String, String>();
		for (int i=0; i<KEYS.length; i++)
		{
			cookies.put(SessionCookies.KEYS[i], "");
		}
	}

	public String getValue(int key)
	{
		if (key >= 0 && key < KEYS.length)
			return cookies.get(KEYS[key]);
		return "";
	}

	public int getIntValue(int key)
	{
		if (key >= 0 && key < KEYS.length)
		{
			try
			{
				int value = Integer.parseInt(cookies.get(KEYS[key]));
				return value;
			}
			catch(Exception e)
			{
				return 0;
			}
		}
		return 0;
	}

	public void setValue(int key, String value)
	{
		if (key >= 0 && key < KEYS.length)
			cookies.put(KEYS[key], value);
	}

	/*
	 *  Return true if no cookies were detected or no valid cookie values
	 */
	public boolean is_empty() 
	{
		String session_id = cookies.get(KEYS[KEY_SESSIONID]);
		if (session_id == null || session_id.length() == 0) 
		{
			return true;
		}
		return false;
	}
}
